package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 用户管理服务实现类
 *
 * @author leizi
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public String sendCode(String phone) {

        // 生成验证码
        String code = "123456";

        // 发送验证码
        log.info("sms send code:{}", code);

        return code;
    }

    @Override
    public User checkUserByPhone(LoginFormDTO loginForm) {
        String phone = loginForm.getPhone();
        return userMapper.checkUserByPhone(phone);
    }

    @Override
    public User registerUser(LoginFormDTO loginForm) {
        User user = new User();
        user.setNickName("黑客-" + UUID.randomUUID().toString().substring(0, 8));
        user.setPhone(loginForm.getPhone());
        user.setPassword("123456");

        userMapper.registerUser(user);
        return user;
    }

    @Override
    public Result getBloggerInfo(Long userId) {
        User user = getById(userId);
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        return Result.ok(userDTO);
    }


}
